package org.HU.train.business.controller.admin.daily;

import org.HU.train.common.response.CommonResp;
import org.HU.train.common.response.PageResp;
import org.HU.train.business.req.daily.DailyTrainTicketQueryReq;
import org.HU.train.business.req.daily.DailyTrainTicketSaveReq;
import org.HU.train.business.resp.daily.DailyTrainTicketQueryResp;
import org.HU.train.business.service.daily.DailyTrainTicketService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * @Author ZUZ
 * @Description: 管理员管理火车票的接口
 * @ClassName: DailyTrainTicketAdminController
 */
@RestController
@RequestMapping("/admin/daily-train-ticket")
public class DailyTrainTicketAdminController {

    @Resource
    private DailyTrainTicketService dailyTrainTicketService;

    @PostMapping("/save")
    public CommonResp<Object> save(@Valid @RequestBody DailyTrainTicketSaveReq req) {
        dailyTrainTicketService.save(req);
        return new CommonResp<>();
    }

    /**
     * 可以根据车次、日期、始发站、终点站查询余票信息
     * 用redis做缓存处理
     */
    @GetMapping("/query-list")
    public CommonResp<PageResp<DailyTrainTicketQueryResp>> queryList(@Valid DailyTrainTicketQueryReq req) {
        PageResp<DailyTrainTicketQueryResp> list = dailyTrainTicketService.queryList(req);
        return new CommonResp<>(list);
    }

    /**
     * 解决第一种缓存穿透：因为TTL到期后缓存消失，大量并发请求访问数据库，导致缓存穿透
     * 设置定时任务，每三十秒执行一次（因为我们设置的redisTTL是60s），以此防止缓存击穿
     */
    @GetMapping("/query-list2")
    public CommonResp<PageResp<DailyTrainTicketQueryResp>> queryList2(@Valid DailyTrainTicketQueryReq req) {
        PageResp<DailyTrainTicketQueryResp> list = dailyTrainTicketService.queryList2(req);
        return new CommonResp<>(list);
    }


    @GetMapping("/query-list3")
    public CommonResp<PageResp<DailyTrainTicketQueryResp>> queryList3(@Valid DailyTrainTicketQueryReq req) {
        PageResp<DailyTrainTicketQueryResp> list = dailyTrainTicketService.queryList3(req);
        return new CommonResp<>(list);
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp<Object> delete(@PathVariable Long id) {
        dailyTrainTicketService.delete(id);
        return new CommonResp<>();
    }

}
